from typing import List, Dict, Tuple


RENEWABLES = ['太阳能', '风能', '水力发电', '地热能', '生物质能']
NON_RENEWABLES = ['煤炭', '石油', '天然气', '核能', '柴油']

HAZARDOUS_WASTE = ['废旧电池', '过期药品', '荧光灯管', '油漆桶', '纽扣电池', '含汞温度计']
NON_HAZARDOUS_WASTE = ['厨余垃圾', '废纸箱', '塑料瓶', '玻璃瓶', '旧衣物', '落叶']

GREENHOUSE_GASES = ['二氧化碳', '甲烷', '一氧化二氮', '臭氧']
NON_GREENHOUSE = ['氧气', '氮气', '氢气', '氦气']

LIGHTING = {
    'correct': ['LED灯'],
    'wrong': ['白炽灯', '卤素灯', '钨丝灯']
}

TRANSPORT_LOW = ['步行', '骑行']
TRANSPORT_HIGH = ['私家车', '汽油摩托车']

BIN_COLOR_RECYCLABLE = '蓝色'
BIN_COLOR_OTHERS = ['红色', '灰色', '绿色']

WATER_DAY = {
    'correct': '3月22日',
    'wrong': ['4月22日', '6月5日', '9月16日']
}

POLICIES = {
    'paris_goal': {
        'q': '《巴黎协定》的核心目标是什么？',
        'options': ['控制全球气温上升', '促进全球贸易', '减少贫困', '促进科技创新'],
        'correct_index': 1
    },
    'dual_carbon': {
        'q': "中国提出的'双碳'目标指的是什么？",
        'options': ['碳达峰和碳中和', '碳交易和碳税', '碳减排和碳吸收', '碳监测和碳报告'],
        'correct_index': 1
    },
    'esg_e': {
        'q': "ESG 投资中的 'E' 代表什么？",
        'options': ['环境', '经济', '教育', '效率'],
        'correct_index': 1
    },
    'circular_3r': {
        'q': "循环经济的三个核心原则是什么？",
        'options': ['减量化、再利用、资源化', '生产、消费、废弃', '开采、制造、销售', '节约、保护、修复'],
        'correct_index': 1
    },
}

OZONE_DEPLETION = {
    'q': '下列哪种物质对臭氧层破坏最大？',
    'options': ['氟利昂', '二氧化碳', '甲烷', '一氧化碳'],
    'correct_index': 1
}


def _compose_options(correct: str, wrong_pool: List[str], seed: int) -> Tuple[List[str], int]:
    wrongs = []
    for j in range(3):
        wrongs.append(wrong_pool[(seed + j) % len(wrong_pool)])
    insert_pos = seed % 4
    options = wrongs.copy()
    options.insert(insert_pos, correct)
    return options, insert_pos + 1


def generate_questions_for_level(level_id: int) -> List[Dict]:
    questions: List[Dict] = []

    # 1-6 简单：垃圾分类、节能灯、低碳出行、可回收桶颜色
    for i in range(2):
        correct = HAZARDOUS_WASTE[i]
        options, ci = _compose_options(correct, NON_HAZARDOUS_WASTE, level_id * 10 + i)
        questions.append({
            'content': f'以下哪项属于有害垃圾？',
            'options': options,
            'correct_option_index': ci,
            'difficulty_label': '简单'
        })

    options, ci = _compose_options(LIGHTING['correct'][0], LIGHTING['wrong'], level_id * 10 + 2)
    questions.append({
        'content': '下列哪种照明方式最节能？',
        'options': options,
        'correct_option_index': ci,
        'difficulty_label': '简单'
    })

    for i in range(2):
        correct = TRANSPORT_LOW[i % len(TRANSPORT_LOW)]
        options, ci = _compose_options(correct, TRANSPORT_HIGH + ['公交车'], level_id * 10 + 3 + i)
        questions.append({
            'content': '哪种出行方式的碳排放最低？',
            'options': options,
            'correct_option_index': ci,
            'difficulty_label': '简单'
        })

    options, ci = _compose_options(BIN_COLOR_RECYCLABLE, BIN_COLOR_OTHERS, level_id * 10 + 5)
    questions.append({
        'content': '可回收垃圾应投放到什么颜色的垃圾桶？',
        'options': options,
        'correct_option_index': ci,
        'difficulty_label': '简单'
    })

    # 7-14 中等：温室气体、可再生能源、水日、蓝色碳汇
    for i in range(3):
        correct = GREENHOUSE_GASES[i]
        options, ci = _compose_options(correct, NON_GREENHOUSE, level_id * 20 + i)
        questions.append({
            'content': '以下哪项属于温室气体？',
            'options': options,
            'correct_option_index': ci,
            'difficulty_label': '中等'
        })

    for i in range(4):
        correct = RENEWABLES[i]
        options, ci = _compose_options(correct, NON_RENEWABLES, level_id * 20 + 3 + i)
        questions.append({
            'content': '以下哪种属于可再生能源？',
            'options': options,
            'correct_option_index': ci,
            'difficulty_label': '中等'
        })

    options, ci = _compose_options(WATER_DAY['correct'], WATER_DAY['wrong'], level_id * 20 + 7)
    questions.append({
        'content': '世界水日是哪一天？',
        'options': options,
        'correct_option_index': ci,
        'difficulty_label': '中等'
    })

    options, ci = _compose_options('红树林', ['森林', '草原', '农田'], level_id * 20 + 8)
    questions.append({
        'content': "下列哪种属于'蓝色碳汇'？",
        'options': options,
        'correct_option_index': ci,
        'difficulty_label': '中等'
    })

    # 15-20 困难：政策类、臭氧层、循环经济、双碳、ESG、巴黎协定
    for key, seed_offset in [('dual_carbon', 0), ('esg_e', 1), ('circular_3r', 2), ('paris_goal', 3)]:
        p = POLICIES[key]
        opts, ci = _compose_options(p['options'][p['correct_index'] - 1],
                                    [o for idx, o in enumerate(p['options']) if idx != p['correct_index'] - 1],
                                    level_id * 30 + seed_offset)
        questions.append({
            'content': p['q'],
            'options': opts,
            'correct_option_index': ci,
            'difficulty_label': '困难'
        })

    opts, ci = _compose_options(OZONE_DEPLETION['options'][OZONE_DEPLETION['correct_index'] - 1],
                                [o for idx, o in enumerate(OZONE_DEPLETION['options']) if idx != OZONE_DEPLETION['correct_index'] - 1],
                                level_id * 30 + 4)
    questions.append({
        'content': OZONE_DEPLETION['q'],
        'options': opts,
        'correct_option_index': ci,
        'difficulty_label': '困难'
    })

    # 增补困难题：节能建筑、绿色出行综合判断
    options, ci = _compose_options('LED灯', ['白炽灯', '卤素灯', '钨丝灯'], level_id * 30 + 5)
    questions.append({
        'content': '在节能建筑照明中优先选择哪种灯具？',
        'options': options,
        'correct_option_index': ci,
        'difficulty_label': '困难'
    })

    options, ci = _compose_options('骑行', ['私家车', '汽油摩托车', '出租车'], level_id * 30 + 6)
    questions.append({
        'content': '综合考虑碳排放与健康，下列出行方式更绿色的是？',
        'options': options,
        'correct_option_index': ci,
        'difficulty_label': '困难'
    })

    return questions


def generate_base_questions() -> List[Dict]:
    all_qs: List[Dict] = []
    for level_id in range(1, 13):
        for q in generate_questions_for_level(level_id):
            all_qs.append({
                'level_id': level_id,
                **q
            })
    return all_qs